Introduction to Python

David Feinberg

Logistics

  • Who:

    • Instructor: David Feinberg
    • TAs: TBD
  • Where:

    • TBD
    • Course Website:
    • Avenue Page:

Expectations & Approach

  • Goal: to learn practical programming in Python
  • How: hands-on, community driven, skills based course, assignment & project driven
  • Lectures & Lab Sections will be used for interactive activities
  • Assignments, coding labs & a final project will be designed to get you coding

Course Requirements

  • Participation in peer instruction (clicker) questions, in lecture (10%)
  • Pass/Fail coding labs, in section (15%)
  • Assignments (50%)
  • Final project (25%)

Why is computer programing being taught in PNB?

  • Programing is an extremely important skill for all sciences.
  • Teaching transferable skills that can be used in and out of academia
  • Producing reproducable science

Programing uses in PNB

How it's used

  • Stimuli creation
  • Data collection
  • Data processing
  • Statistical analysis

Real Examples Actually Used In PNB

  • Driving simulator
  • Statistical analysis
  • Data visualization
  • Machine Learning
  • EEG Analysis
  • Acoustical Analysis
  • Voice manipulations
  • PsychoPy for data collection

Why Python instead of another language?

  • Python is a high level scripting language
    • You can start doing complex things quickly
  • Relatively easy to learn
    • Programming skills can be transferred to any language
  • Many libraries available for science
    • Don't reinvent the wheel
  • Python ecosystem
    • A culture of sharing and helpfulness
  • Python is open-source and accessible
    • Open-source means anyone can look under the hood, see how things are made, fix them, copy them, or even repackage and sell off the parts for profit.

Why is it called Python?

  • It is named after British comedy group Monty Python

What does Python look like?

In [24]:
print("Hello, world!")
Hello, world!
In [25]:
a = 1
b = 2

c = a + b

print(c)
3
In [20]:
import math

math.sqrt(81)
Out[20]:
9.0

That's great, we can do math...

I can do that on my phone, so how is this usefull for PNB again?

In [26]:
import statistics

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Mean of x: ", statistics.mean(x))
print("Standard deviation of x: ", statistics.stdev(x))
print("Variance of x: ", statistics.variance(x))
Mean of x:  5
Standard deviation of x:  3.3166247903554
Variance of x:  11
In [8]:
from scipy.stats.stats import pearsonr

a = [1, 4, 6, 8, 10, 12]
b = [1, 2, 3, 4, 5, 2]   
r, p = pearsonr(a,b)
print("The correlation coefficient between a & b is r = {}".format(r))
print("The p-value of that correlation is p = {}".format(p))
if p < 0.05:
    print("The correlation is significant at p < 0.5")
elif p > 0.05:
    print("The correlation is not significant at p < 0.5")
The correlation coefficient between a & b is r = 0.568844844207
The p-value of that correlation is p = 0.238767408643
The correlation is not significant at p < 0.5
In [9]:
import matplotlib
import matplotlib.pyplot as plt


plt.scatter(a, b)
plt.show()

YouTube_dl

  • A thesis student in my lab once analyzed the frequency components of different types of songs for her project
  • Python allowed us to download and analyze all of the songs from YouTube automatically, and then analyze the acoustic properties of the songs
  • Here is the example code...

Download the song

In [24]:
import youtube_dl
import pandas as pd

song_list = [
    'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
]

ydl_opts = {
    'format': 'best',
    'outtmpl': 'my_favourite_song.webm'
}

for song in song_list:
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([song])
        
[youtube] dQw4w9WgXcQ: Downloading webpage
[youtube] dQw4w9WgXcQ: Downloading video info webpage
[download] Destination: my_favourite_song.webm
[download] 100% of 15.18MiB in 00:0121MiB/s ETA 00:009

Play the song

In [26]:
import io
import base64
from IPython.display import HTML

video = io.open('my_favourite_song.webm', 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''<video alt="test" controls>
                <source src="data:video/webm;base64,{0}" type="video/webm" />
             </video>'''.format(encoded.decode('ascii')))
Out[26]:
In [ ]: